package scc.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import scc.project.dao.CustomDao;
import scc.project.entity.Custom;
import scc.project.entity.vo.PageVo;

import java.util.List;

/**
 * 客户的业务类
 *
 * @author 陈圳佳
 *         日期：2015/6/22
 */
@Service
public class CustomService {

    @Autowired
    private CustomDao customDao;

    /**
     * 根据条件查找
     *
     * @param page       页码
     * @param address    客户的地址
     * @param customName 客户的名称
     */
    public PageVo<Custom> find(Integer page, String address, String customName) {
        PageVo<Custom> pageVo = new PageVo<Custom>(page);
        List<Custom> customs = customDao.find(pageVo.getOffset(), pageVo.getRows(), address, customName);
        pageVo.setData(customs);
        pageVo.addParam("address", address);
        pageVo.addParam("name", customName);
        Long total = customDao.findTotal(address, customName);
        pageVo.setTotal(total);
        return pageVo;
    }

    /**
     * 添加
     *
     * @param custom 客户对象
     * @return 成功返回 true，失败则false
     */
    public boolean save(Custom custom) {
        return customDao.insert(custom) > 0;
    }

    /**
     * 根据ID 拿到用户对象
     *
     * @param id id
     * @return 用户对象
     */
    public Custom get(Long id) {
        return customDao.selectById(id);
    }

    public boolean updateById(Custom custom) {
        return customDao.updateById(custom) > 0;
    }


    public boolean delete(Long id) {
        return customDao.deleteById(id) > 0;
    }

    public List<Custom> getAll() {
        return customDao.selectAll();
    }
}
